package com.myday.system.controller;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.myday.system.domain.dto.TimerAddDTO;
import com.myday.system.domain.vo.MyTimerVO;
import com.myday.system.service.IMyTodoService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.myday.system.domain.MyTimer;
import com.myday.system.service.IMyTimerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 计时器Controller
 *
 * @author xiaofengs
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/system/timer")
public class MyTimerController extends BaseController {
    @Autowired
    private IMyTimerService myTimerService;

    @Autowired
    private IMyTodoService myTodoService;
    /**
     * 查询计时器列表
     */
    @PreAuthorize("@ss.hasPermi('system:timer:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyTimer myTimer) {
        startPage();
        // 查询改用户的计时器列表
        List<MyTimer> list = myTimerService.selectMyTimerListByUserID(myTimer, SecurityUtils.getUserId());
        // 更新计时器状态
        for (MyTimer timer : list) {

            timer.setTodoTitle(myTodoService.selectMyTodoByTodoId(timer.getTodoId()).getTitle());
            if (timer.getHaveStop() != 1) {
                if (timer.getUpdateTime() != null) {
                    timer.setDuration(timer.getDuration()+
                            (int)((
                                    LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - timer.getUpdateTime().toEpochSecond(ZoneOffset.UTC)
                    )/60));
                }else{
                    timer.setDuration(
                            (int)((
                                    LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - timer.getCreateTime().toEpochSecond(ZoneOffset.UTC)
                    )/60));
                }
                myTimerService.updateMyTimer(timer);
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取计时器详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:timer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(myTimerService.selectMyTimerById(id));
    }

    /**
     * 新增计时器
     */
    @PreAuthorize("@ss.hasPermi('system:timer:add')")
    @Log(title = "计时器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TimerAddDTO timerAddDTO) {
        MyTimer myTimer = new MyTimer();
        myTimer.setTodoId(timerAddDTO.getTodoId());
        myTimer.setCreateTime(LocalDateTime.now());
        myTimer.setDuration(0);
        myTimer.setUpdateTime(null);
        return toAjax(myTimerService.insertMyTimer(myTimer));
    }

    /**
     * Todo是否存在计时器
     *
     * @param timerAddDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:timer:add')")
    @Log(title = "计时器", businessType = BusinessType.INSERT)
    @GetMapping("/have/{todoId}")
    public AjaxResult have(@PathVariable Long todoId) {
        int count = myTimerService.selectMyTimerByTodoId(todoId).size();
        return success((Object) count);
    }

    /**
     * 中止计时器
     */
    @PreAuthorize("@ss.hasPermi('system:timer:edit')")
    @Log(title = "计时器", businessType = BusinessType.UPDATE)
    @GetMapping("/stop/{id}")
    public AjaxResult edit(@PathVariable Long id) {
        MyTimer myTimer = new MyTimer();
        myTimer.setId(id);

        MyTimer timer = myTimerService.selectMyTimerById(id);
        myTimer.setHaveStop((short) 1);
        if (timer.getUpdateTime() != null) {
            myTimer.setDuration(timer.getDuration() + (int) (LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - timer.getUpdateTime().toEpochSecond(ZoneOffset.UTC)) / 60);
        }
        if (timer.getDuration() == 0) {
            myTimer.setDuration((int) (LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - timer.getCreateTime().toEpochSecond(ZoneOffset.UTC)) / 60);
        }
        myTimer.setUpdateTime(LocalDateTime.now());
        return toAjax(myTimerService.updateMyTimer(myTimer));
    }

    /**
     * 继续计时器
     */
    @PreAuthorize("@ss.hasPermi('system:timer:edit')")
    @Log(title = "计时器", businessType = BusinessType.UPDATE)
    @GetMapping("/continue/{id}")
    public AjaxResult continuing(@PathVariable Long id) {
        MyTimer myTimer = new MyTimer();
        myTimer.setId(id);
        myTimer.setUpdateTime(LocalDateTime.now());
        myTimer.setHaveStop((short) 0);
        myTimer.setUpdateTime(LocalDateTime.now());
        return toAjax(myTimerService.updateMyTimer(myTimer));
    }

    /**
     * 删除计时器
     */
    @PreAuthorize("@ss.hasPermi('system:timer:remove')")
    @Log(title = "计时器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(myTimerService.deleteMyTimerByIds(ids));
    }
}
